/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao;

import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author 39370
 */
public class Fenlei {

    private static int SUCCESS = 1;
    private static int ERROR = 2;
    private static int SAME = 3;

    public List getFenLei() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();
        // 异常处理
        try {
            // 执行SQL语句
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from bookclassification order by bookclass_id desc");
            rs = ps.executeQuery();
            while (rs.next()) {
                // 数组容器类对象-中间对象
                List list2 = new ArrayList();
                // 接收此书籍分类信息
                list2.add(rs.getString(1));
                list2.add(rs.getString(2));
                list2.add(rs.getString(3));
                // 将此书籍分类信息加入到书籍信息库
                list.add(list2);
            }
            // 返回书籍分类信息库
            return list;
        } catch (Exception e) { //捕获异常
            // 返回书籍分类信息库
            return list;
        } finally { // 最后一定执行代码
            // 关闭数据库
            DatabaseUtil.close(rs, ps, con);
        }
    }

    // 添加分类信息
    public int addFenLei(String bookclass_classification) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
         Random random = new Random();

        int r = random.nextInt(100);
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from bookclassification where bookclass_classification=?");
            ps.setString(1, bookclass_classification);
            rs = ps.executeQuery();
            if (rs.next()) { // 有重名
                // 返回重名标记
                return this.SAME;
            } else { //无重名
                // 执行SQL更新操作-添加
                ps = con.prepareStatement("insert into bookclassification(bookclass_id,bookclass_classification,bookclass_addtime) values(?,?,?)");
                
                ps.setInt(1, r);
                ps.setString(2, bookclass_classification);
                ps.setString(3, new java.util.Date().toLocaleString());
                int i = ps.executeUpdate();
                if (i == 1) { //添加成功
                    // 返回执行成功标记
                    return this.SUCCESS;
                } else { //添加失败
                    // 返回执行失败标记
                    return this.ERROR;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally { 
            DatabaseUtil.close(rs, ps, con);
        }
    }

    // 删除分类信息
    public int delFenLei(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 执行SQL语句-删除
            con = DatabaseUtil.getConnection();
                ps = con.prepareStatement("delete from bookclassification where bookclass_id=?");
                ps.setInt(1, id);
                ps.executeUpdate();
            
            return this.SUCCESS;
        } catch (Exception e) { 
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }
}
